#!/bin/bash

echo "📊 Monitoring System Verification"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 检查 Prometheus 指标
echo -n "Checking Prometheus metrics... "
if curl -s http://localhost:3000/api/metrics | grep -q "baihu_requests_total"; then
  echo -e "${GREEN}✓${NC}"
else
  echo -e "${RED}✗${NC}"
fi

# 检查日志文件
echo -n "Checking log files... "
if [ -f "./logs/$(date +%Y-%m-%d).log" ]; then
  echo -e "${GREEN}✓${NC}"
else
  echo -e "${YELLOW}⚠ (no logs for today)${NC}"
fi

# 检查健康状态
echo -n "Checking health status... "
health_status=$(curl -s http://localhost:3000/api/health | jq -r '.status' 2>/dev/null || echo "unknown")
if [ "$health_status" = "healthy" ]; then
  echo -e "${GREEN}✓${NC}"
else
  echo -e "${RED}✗ (status: $health_status)${NC}"
fi

# 检查审计日志
echo -n "Checking audit logs... "
if [ -f "./data/audit.log" ]; then
  echo -e "${GREEN}✓${NC}"
else
  echo -e "${YELLOW}⚠ (no audit logs found)${NC}"
fi

# 检查内存使用
echo -n "Checking memory usage... "
memory_used=$(curl -s http://localhost:3000/api/health | jq -r '.system.memory.used' 2>/dev/null || echo "0")
if [ "$memory_used" -gt 0 ] && [ "$memory_used" -lt 500 ]; then
  echo -e "${GREEN}✓ (${memory_used}MB)${NC}"
else
  echo -e "${YELLOW}⚠ (${memory_used}MB)${NC}"
fi

echo "Monitoring verification completed!"
